package com.android.lovingpet.mine.pet;

import android.os.Bundle;
import android.support.annotation.Nullable;

import com.android.lovingpet.base.BasePresenter;
import com.android.lovingpet.bean.Pet;
import com.android.lovingpet.common.Back;
import com.android.lovingpet.common.FileModel;
import com.android.lovingpet.util.MyHttp;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

/**
 * Created by ikata on 2018/4/29.
 */

public class PetPresenter extends BasePresenter<IPetContract.View>implements IPetContract.Presenter {

    private PetModel petModel;
    public PetPresenter()
    {
        this.petModel=new PetModel();
    }
    @Override
    public void addPet(String name, String type, String birthday, int sterilize, int gender,File head) {
        HashMap<String,String> params=new HashMap<>();
        params.put("name",name);
        params.put("type",type);
        params.put("birthday",birthday);
        params.put("sterilize", String.valueOf(sterilize));
        params.put("gender", String.valueOf(gender));
        petModel.addPet(params, new Back() {
            @Override
            public void onFailure(String msg) {
                if (getView()!=null)
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                assert bundle != null;
                int id=bundle.getInt("id");
                uploadHead(id,head);
                if (getView() != null) {
                    getView().next();
                }
            }
        });
    }

    @Override
    public void getPetId(int masterId, int petType, int offset, int num,int chooseType) {
        HashMap<String,String> params=new HashMap<>();
        params.put("masterId", String.valueOf(masterId));
        params.put("petType", String.valueOf(petType));
        params.put("offset", String.valueOf(offset));
        params.put("num", String.valueOf(num));
        params.put("chooseType", String.valueOf(chooseType));
        petModel.getPetId(params, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                ArrayList<Integer> list= bundle.getIntegerArrayList("ids");
                if (list != null) {
                    for (Integer s : list) {
                        getPetInfo(s+"","0 ");
                    }
                }
            }
        });
    }

    @Override
    public void getPetInfo(String id, String petType) {
        HashMap<String,String> params=new HashMap<>();
        params.put("id",id);
        params.put("petType",petType);
        petModel.getPetInfo(params, new Back() {
            @Override
            public void onFailure(String msg) {

                getView().showToast(msg);
                getView().hideProgress();
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                Pet pet=bundle.getParcelable("result");
                getView().showMyPet(pet);
            }
        });
    }

    @Override
    public void deletePet(int id) {
        petModel.deletePet(String.valueOf(id), new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }
            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                if (bundle==null)
                {
                    getView().next();
                }
            }
        });
    }

    @Override
    public void uploadHead(int id,File file) {
        FileModel.getInstance().uploadImage(id, 2, file, new Back() {
            @Override
            public void onFailure(String msg) {
                getView().showToast(msg);
            }

            @Override
            public void onSuccess(@Nullable Bundle bundle) {
                getView().showToast("添加成功");
            }
        });
    }


}
